<!-- HTML header for doxygen 1.8.6-->
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/xhtml;charset=UTF-8"/>
<meta http-equiv="X-UA-Compatible" content="IE=9"/>
<meta name="generator" content="Doxygen 1.8.13"/>
<title>OpenCV: Creating a 3D histogram</title>
<link href="../../opencv.ico" rel="shortcut icon" type="image/x-icon" />
<link href="../../tabs.css" rel="stylesheet" type="text/css"/>
<script type="text/javascript" src="../../jquery.js"></script>
<script type="text/javascript" src="../../dynsections.js"></script>
<script type="text/javascript" src="../../tutorial-utils.js"></script>
<link href="../../search/search.css" rel="stylesheet" type="text/css"/>
<script type="text/javascript" src="../../search/searchdata.js"></script>
<script type="text/javascript" src="../../search/search.js"></script>
<script type="text/x-mathjax-config">
  MathJax.Hub.Config({
    extensions: ["tex2jax.js", "TeX/AMSmath.js", "TeX/AMSsymbols.js"],
    jax: ["input/TeX","output/HTML-CSS"],
});
//<![CDATA[
MathJax.Hub.Config(
{
  TeX: {
      Macros: {
          matTT: [ "\\[ \\left|\\begin{array}{ccc} #1 & #2 & #3\\\\ #4 & #5 & #6\\\\ #7 & #8 & #9 \\end{array}\\right| \\]", 9],
          fork: ["\\left\\{ \\begin{array}{l l} #1 & \\mbox{#2}\\\\ #3 & \\mbox{#4}\\\\ \\end{array} \\right.", 4],
          forkthree: ["\\left\\{ \\begin{array}{l l} #1 & \\mbox{#2}\\\\ #3 & \\mbox{#4}\\\\ #5 & \\mbox{#6}\\\\ \\end{array} \\right.", 6],
          forkfour: ["\\left\\{ \\begin{array}{l l} #1 & \\mbox{#2}\\\\ #3 & \\mbox{#4}\\\\ #5 & \\mbox{#6}\\\\ #7 & \\mbox{#8}\\\\ \\end{array} \\right.", 8],
          vecthree: ["\\begin{bmatrix} #1\\\\ #2\\\\ #3 \\end{bmatrix}", 3],
          vecthreethree: ["\\begin{bmatrix} #1 & #2 & #3\\\\ #4 & #5 & #6\\\\ #7 & #8 & #9 \\end{bmatrix}", 9],
          cameramatrix: ["#1 = \\begin{bmatrix} f_x & 0 & c_x\\\\ 0 & f_y & c_y\\\\ 0 & 0 & 1 \\end{bmatrix}", 1],
          distcoeffs: ["(k_1, k_2, p_1, p_2[, k_3[, k_4, k_5, k_6 [, s_1, s_2, s_3, s_4[, \\tau_x, \\tau_y]]]]) \\text{ of 4, 5, 8, 12 or 14 elements}"],
          distcoeffsfisheye: ["(k_1, k_2, k_3, k_4)"],
          hdotsfor: ["\\dots", 1],
          mathbbm: ["\\mathbb{#1}", 1],
          bordermatrix: ["\\matrix{#1}", 1]
      }
  }
}
);
//]]>
</script><script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/mathjax/2.7.0/MathJax.js"></script>
<link href="../../doxygen.css" rel="stylesheet" type="text/css" />
<link href="../../stylesheet.css" rel="stylesheet" type="text/css"/>
</head>
<body>
<div id="top"><!-- do not remove this div, it is closed by doxygen! -->
<div id="titlearea">
<!--#include virtual="/google-search.html"-->
<table cellspacing="0" cellpadding="0">
 <tbody>
 <tr style="height: 56px;">
  <td id="projectlogo"><img alt="Logo" src="../../opencv-logo-small.png"/></td>
  <td style="padding-left: 0.5em;">
   <div id="projectname">OpenCV
   &#160;<span id="projectnumber">4.5.2</span>
   </div>
   <div id="projectbrief">Open Source Computer Vision</div>
  </td>
 </tr>
 </tbody>
</table>
</div>
<!-- end header part -->
<!-- Generated by Doxygen 1.8.13 -->
<script type="text/javascript">
var searchBox = new SearchBox("searchBox", "../../search",false,'Search');
</script>
<script type="text/javascript" src="../../menudata.js"></script>
<script type="text/javascript" src="../../menu.js"></script>
<script type="text/javascript">
$(function() {
  initMenu('../../',true,false,'search.php','Search');
  $(document).ready(function() { init_search(); });
});
</script>
<div id="main-nav"></div>
<!-- window showing the filter options -->
<div id="MSearchSelectWindow"
     onmouseover="return searchBox.OnSearchSelectShow()"
     onmouseout="return searchBox.OnSearchSelectHide()"
     onkeydown="return searchBox.OnSearchSelectKey(event)">
</div>

<!-- iframe showing the search results (closed by default) -->
<div id="MSearchResultsWindow">
<iframe src="javascript:void(0)" frameborder="0" 
        name="MSearchResults" id="MSearchResults">
</iframe>
</div>

<div id="nav-path" class="navpath">
  <ul>
<li class="navelem"><a class="el" href="../../d3/d81/tutorial_contrib_root.html">Tutorials for contrib modules</a></li><li class="navelem"><a class="el" href="../../d7/df9/tutorial_table_of_content_viz.html">OpenCV Viz</a></li>  </ul>
</div>
</div><!-- top -->
<div class="header">
  <div class="headertitle">
<div class="title">Creating a 3D histogram </div>  </div>
</div><!--header-->
<div class="contents">
<div class="textblock"><p><b>Prev Tutorial:</b> <a class="el" href="../../d2/d64/tutorial_creating_widgets.html">Creating Widgets</a></p>
<h2>Goal </h2>
<p>In this tutorial you will learn how to</p>
<ul>
<li>Create your own callback keyboard function for viz window.</li>
<li>Show your 3D histogram in a viz window.</li>
</ul>
<h2>Code </h2>
<p>You can download the code from <a href="https://github.com/opencv/opencv_contrib/tree/master/modules/viz/samples/histo3D.cpp">here</a>. </p><div class="fragment"><div class="line"><span class="preprocessor">#include &lt;<a class="code" href="../../d0/d9c/core_2include_2opencv2_2core_8hpp.html">opencv2/core.hpp</a>&gt;</span></div><div class="line"><span class="preprocessor">#include &lt;<a class="code" href="../../d1/d4f/imgproc_2include_2opencv2_2imgproc_8hpp.html">opencv2/imgproc.hpp</a>&gt;</span></div><div class="line"><span class="preprocessor">#include &lt;<a class="code" href="../../d4/dd5/highgui_8hpp.html">opencv2/highgui.hpp</a>&gt;</span></div><div class="line"><span class="preprocessor">#include &lt;iostream&gt;</span></div><div class="line"></div><div class="line"><span class="keyword">using namespace </span>std;</div><div class="line"><span class="keyword">using namespace </span><a class="code" href="../../d2/d75/namespacecv.html">cv</a>;</div><div class="line"></div><div class="line"><span class="preprocessor">#ifdef HAVE_OPENCV_VIZ</span></div><div class="line"></div><div class="line"><span class="preprocessor">#include &lt;<a class="code" href="../../d0/d7e/viz_8hpp.html">opencv2/viz.hpp</a>&gt;</span></div><div class="line"></div><div class="line"><span class="keyword">const</span> <a class="code" href="../../dc/d84/group__core__basic.html#ga1f6634802eeadfd7245bc75cf3e216c2">String</a> keys =</div><div class="line"><span class="stringliteral">&quot;{Aide h usage ? help  |     | print this message   }&quot;</span></div><div class="line"><span class="stringliteral">&quot;{@arg1                |     | Full path to color imag (3 channels)}&quot;</span></div><div class="line">;</div><div class="line"></div><div class="line"></div><div class="line"><span class="keyword">struct </span>Histo3DData {</div><div class="line">    <a class="code" href="../../d3/d63/classcv_1_1Mat.html">Mat</a> histogram;</div><div class="line">    <span class="keywordtype">int</span> seuil;</div><div class="line">    <span class="keywordtype">double</span> <a class="code" href="../../d7/d1b/group__imgproc__misc.html#gae8a4a146d1ca78c626a53577199e9c57">threshold</a>;</div><div class="line">    <a class="code" href="../../dc/d84/group__core__basic.html#ga6395ca871a678020c4a31fadf7e8cc63">Ptr&lt;viz::Viz3d&gt;</a> fen3D;</div><div class="line">    <span class="keywordtype">int</span> nbWidget;</div><div class="line">    <span class="keywordtype">bool</span> status;</div><div class="line">    <span class="keywordtype">double</span> maxH;</div><div class="line">    <span class="keywordtype">int</span> code;</div><div class="line">};</div><div class="line"></div><div class="line"><span class="keywordtype">void</span> DrawHistogram3D(Histo3DData &amp;);</div><div class="line"><span class="keywordtype">void</span> AddSlidebar(<a class="code" href="../../dc/d84/group__core__basic.html#ga1f6634802eeadfd7245bc75cf3e216c2">String</a> sliderName, <a class="code" href="../../dc/d84/group__core__basic.html#ga1f6634802eeadfd7245bc75cf3e216c2">String</a> windowName, <span class="keywordtype">int</span> sliderMin, <span class="keywordtype">int</span> sliderMax, <span class="keywordtype">int</span> valeurDefaut, <span class="keywordtype">int</span> *sliderVal, <span class="keywordtype">void</span>(*f)(<span class="keywordtype">int</span>, <span class="keywordtype">void</span> *), <span class="keywordtype">void</span> *r);</div><div class="line"><span class="keywordtype">void</span> UpdateThreshold(<span class="keywordtype">int</span> , <span class="keywordtype">void</span> * r);</div><div class="line"><span class="keywordtype">void</span>  KeyboardViz3d(<span class="keyword">const</span> <a class="code" href="../../d3/d06/classcv_1_1viz_1_1KeyboardEvent.html">viz::KeyboardEvent</a> &amp;w, <span class="keywordtype">void</span> *t);</div><div class="line"></div><div class="line"></div><div class="line"><span class="keywordtype">void</span> DrawHistogram3D(Histo3DData &amp;h)</div><div class="line">{</div><div class="line">    <span class="keywordtype">int</span> planSize = (int)h.histogram.step1(0);</div><div class="line">    <span class="keywordtype">int</span> cols = (int)h.histogram.step1(1);</div><div class="line">    <span class="keywordtype">int</span> rows = (int)planSize / cols;</div><div class="line">    <span class="keywordtype">int</span> plans = (int)h.histogram.total() / planSize;</div><div class="line">    h.fen3D-&gt;removeAllWidgets();</div><div class="line">    h.nbWidget=0;</div><div class="line">    <span class="keywordflow">if</span> (h.nbWidget==0)</div><div class="line">        h.fen3D-&gt;showWidget(<span class="stringliteral">&quot;Axis&quot;</span>, <a class="code" href="../../df/d63/classcv_1_1viz_1_1WCoordinateSystem.html">viz::WCoordinateSystem</a>(10));</div><div class="line">    <span class="keywordflow">for</span> (<span class="keywordtype">int</span> k = 0; k &lt; plans; k++)</div><div class="line">    {</div><div class="line">        <span class="keywordflow">for</span> (<span class="keywordtype">int</span> i = 0; i &lt; rows; i++)</div><div class="line">        {</div><div class="line">            <span class="keywordflow">for</span> (<span class="keywordtype">int</span> j = 0; j &lt; cols; j++)</div><div class="line">            {</div><div class="line">                <span class="keywordtype">double</span> x = h.histogram.at&lt;<span class="keywordtype">float</span>&gt;(k, i, j);</div><div class="line">                <span class="keywordflow">if</span> (x &gt;= h.threshold)</div><div class="line">                {</div><div class="line">                    <span class="keywordtype">double</span> r=<a class="code" href="../../d7/dcc/group__core__utils__softfloat.html#ga78f988f6cfa6223610298cbd4f86ec66">std::max</a>(x/h.maxH,0.1);</div><div class="line">                    <a class="code" href="../../d2/d17/classcv_1_1viz_1_1WCube.html">viz::WCube</a> s(<a class="code" href="../../dc/d84/group__core__basic.html#ga17f8af95b76535bd387f4909850c8810">Point3d</a>(k - r / 2, i - r / 2, j - r / 2), <a class="code" href="../../dc/d84/group__core__basic.html#ga17f8af95b76535bd387f4909850c8810">Point3d</a>(k + r / 2, i + r / 2, j + r / 2), <span class="keyword">false</span>, <a class="code" href="../../d4/dba/classcv_1_1viz_1_1Color.html">viz::Color</a>(j / <span class="keywordtype">double</span>(plans) * 255, i / <span class="keywordtype">double</span>(rows) * 255, k / <span class="keywordtype">double</span>(cols) * 255));</div><div class="line">                    h.fen3D-&gt;showWidget(format(<span class="stringliteral">&quot;I3d%d&quot;</span>, h.nbWidget++), s);</div><div class="line">                }</div><div class="line">            }</div><div class="line">        }</div><div class="line">    }</div><div class="line">    h.status = <span class="keyword">false</span>;</div><div class="line">}</div><div class="line"><span class="keywordtype">void</span>  KeyboardViz3d(<span class="keyword">const</span> <a class="code" href="../../d3/d06/classcv_1_1viz_1_1KeyboardEvent.html">viz::KeyboardEvent</a> &amp;w, <span class="keywordtype">void</span> *t)</div><div class="line">{</div><div class="line">   Histo3DData *x=(Histo3DData *)t;</div><div class="line">   <span class="keywordflow">if</span> (w.<a class="code" href="../../d3/d06/classcv_1_1viz_1_1KeyboardEvent.html#a6397bf651511d452f26a700df2df7381">action</a>)</div><div class="line">       cout &lt;&lt; <span class="stringliteral">&quot;you pressed &quot;</span>&lt;&lt; w.<a class="code" href="../../d3/d06/classcv_1_1viz_1_1KeyboardEvent.html#a45265d0feab99428177831a24374854f">symbol</a>&lt;&lt; <span class="stringliteral">&quot; in viz window &quot;</span>&lt;&lt;x-&gt;fen3D-&gt;getWindowName()&lt;&lt;<span class="stringliteral">&quot;\n&quot;</span>;</div><div class="line">   x-&gt;code= w.<a class="code" href="../../d3/d06/classcv_1_1viz_1_1KeyboardEvent.html#a90f5e8b6d152aa7f1ecf4a5098020c74">code</a>;</div><div class="line">   <span class="keywordflow">switch</span> (w.<a class="code" href="../../d3/d06/classcv_1_1viz_1_1KeyboardEvent.html#a90f5e8b6d152aa7f1ecf4a5098020c74">code</a>) {</div><div class="line">   <span class="keywordflow">case</span> <span class="charliteral">&#39;/&#39;</span>:</div><div class="line">           x-&gt;status=<span class="keyword">true</span>;</div><div class="line">           x-&gt;threshold *= 0.9;</div><div class="line">       <span class="keywordflow">break</span>;</div><div class="line">   <span class="keywordflow">case</span> <span class="charliteral">&#39;*&#39;</span>:</div><div class="line">       x-&gt;status = <span class="keyword">true</span>;</div><div class="line">           x-&gt;threshold *= 1.1;</div><div class="line">       <span class="keywordflow">break</span>;</div><div class="line">    }</div><div class="line">   <span class="keywordflow">if</span> (x-&gt;status)</div><div class="line">   {</div><div class="line">       cout &lt;&lt;  x-&gt;threshold &lt;&lt; <span class="stringliteral">&quot;\n&quot;</span>;</div><div class="line">       DrawHistogram3D(*x);</div><div class="line">   }</div><div class="line">}</div><div class="line"></div><div class="line"></div><div class="line"><span class="keywordtype">void</span> AddSlidebar(<a class="code" href="../../dc/d84/group__core__basic.html#ga1f6634802eeadfd7245bc75cf3e216c2">String</a> sliderName, <a class="code" href="../../dc/d84/group__core__basic.html#ga1f6634802eeadfd7245bc75cf3e216c2">String</a> windowName, <span class="keywordtype">int</span> sliderMin, <span class="keywordtype">int</span> sliderMax, <span class="keywordtype">int</span> defaultSlider, <span class="keywordtype">int</span> *sliderVal, <span class="keywordtype">void</span>(*f)(<span class="keywordtype">int</span>, <span class="keywordtype">void</span> *), <span class="keywordtype">void</span> *r)</div><div class="line">{</div><div class="line">    <a class="code" href="../../d7/dfc/group__highgui.html#gaf78d2155d30b728fc413803745b67a9b">createTrackbar</a>(sliderName, windowName, sliderVal, 1, f, r);</div><div class="line">    <a class="code" href="../../d7/dfc/group__highgui.html#gabe26ffe8d2b60cc678895595a581b7aa">setTrackbarMin</a>(sliderName, windowName, sliderMin);</div><div class="line">    <a class="code" href="../../d7/dfc/group__highgui.html#ga7e5437ccba37f1154b65210902fc4480">setTrackbarMax</a>(sliderName, windowName, sliderMax);</div><div class="line">    <a class="code" href="../../d7/dfc/group__highgui.html#ga67d73c4c9430f13481fd58410d01bd8d">setTrackbarPos</a>(sliderName, windowName, defaultSlider);</div><div class="line">}</div><div class="line"></div><div class="line"></div><div class="line"><span class="keywordtype">void</span> UpdateThreshold(<span class="keywordtype">int</span> , <span class="keywordtype">void</span> * r)</div><div class="line">{</div><div class="line">    Histo3DData *h = (Histo3DData *)r;</div><div class="line">    h-&gt;status=<span class="keyword">true</span>;</div><div class="line">    h-&gt;threshold = h-&gt;seuil/1000000.0;</div><div class="line">    cout&lt;&lt;<span class="stringliteral">&quot;Widget : &quot;</span>&lt;&lt;h-&gt;nbWidget&lt;&lt;<span class="stringliteral">&quot;,&quot;</span>&lt;&lt; h-&gt;threshold&lt;&lt;<span class="stringliteral">&quot;\n&quot;</span>;</div><div class="line">}</div><div class="line"></div><div class="line"><span class="keywordtype">int</span> main (<span class="keywordtype">int</span> argc,<span class="keywordtype">char</span> **argv)</div><div class="line">{</div><div class="line">    <a class="code" href="../../d0/d2e/classcv_1_1CommandLineParser.html">CommandLineParser</a> parser(argc, argv, keys);</div><div class="line"></div><div class="line">    <span class="keywordflow">if</span> (parser.has(<span class="stringliteral">&quot;help&quot;</span>))</div><div class="line">    {</div><div class="line">        parser.printMessage();</div><div class="line">        <span class="keywordflow">return</span> 0;</div><div class="line">    }</div><div class="line">    <a class="code" href="../../dc/d84/group__core__basic.html#ga1f6634802eeadfd7245bc75cf3e216c2">String</a> nomFic = parser.get&lt;<a class="code" href="../../dc/d84/group__core__basic.html#ga1f6634802eeadfd7245bc75cf3e216c2">String</a>&gt;(0);</div><div class="line">    <a class="code" href="../../d3/d63/classcv_1_1Mat.html">Mat</a> img;</div><div class="line">    <span class="keywordflow">if</span> (nomFic.length() != 0)</div><div class="line">    {</div><div class="line">        img = <a class="code" href="../../d4/da8/group__imgcodecs.html#ga288b8b3da0892bd651fce07b3bbd3a56">imread</a>(nomFic, <a class="code" href="../../d8/d6a/group__imgcodecs__flags.html#gga61d9b0126a3e57d9277ac48327799c80af660544735200cbe942eea09232eb822">IMREAD_COLOR</a>);</div><div class="line">        <span class="keywordflow">if</span> (img.empty())</div><div class="line">        {</div><div class="line">            cout &lt;&lt; <span class="stringliteral">&quot;Image does not exist!&quot;</span>;</div><div class="line">            <span class="keywordflow">return</span> 0;</div><div class="line">        }</div><div class="line">    }</div><div class="line">    <span class="keywordflow">else</span></div><div class="line">    {</div><div class="line">        img = <a class="code" href="../../d3/d63/classcv_1_1Mat.html">Mat</a>(512,512,<a class="code" href="../../d1/d1b/group__core__hal__interface.html#ga88c4cd9de76f678f33928ef1e3f96047">CV_8UC3</a>);</div><div class="line">        parser.printMessage();</div><div class="line">        <a class="code" href="../../d1/dd6/classcv_1_1RNG.html">RNG</a> r;</div><div class="line">        r.<a class="code" href="../../d1/dd6/classcv_1_1RNG.html#ad26f2b09d9868cf108e84c9814aa682d">fill</a>(img(<a class="code" href="../../dc/d84/group__core__basic.html#ga11d95de507098e90bad732b9345402e8">Rect</a>(0, 0, 256, 256)), RNG::NORMAL, <a class="code" href="../../dc/d84/group__core__basic.html#ga7e6060c0b8d48459964df6e1eb524c03">Vec3b</a>(60, 40, 50), <a class="code" href="../../dc/d84/group__core__basic.html#ga7e6060c0b8d48459964df6e1eb524c03">Vec3b</a>(10, 5, 20));</div><div class="line">        r.<a class="code" href="../../d1/dd6/classcv_1_1RNG.html#ad26f2b09d9868cf108e84c9814aa682d">fill</a>(img(<a class="code" href="../../dc/d84/group__core__basic.html#ga11d95de507098e90bad732b9345402e8">Rect</a>(256, 0, 256, 256)), RNG::NORMAL, <a class="code" href="../../dc/d84/group__core__basic.html#ga7e6060c0b8d48459964df6e1eb524c03">Vec3b</a>(160, 10, 50), <a class="code" href="../../dc/d84/group__core__basic.html#ga7e6060c0b8d48459964df6e1eb524c03">Vec3b</a>(20, 5, 10));</div><div class="line">        r.<a class="code" href="../../d1/dd6/classcv_1_1RNG.html#ad26f2b09d9868cf108e84c9814aa682d">fill</a>(img(<a class="code" href="../../dc/d84/group__core__basic.html#ga11d95de507098e90bad732b9345402e8">Rect</a>(0, 256, 256, 256)), RNG::NORMAL, <a class="code" href="../../dc/d84/group__core__basic.html#ga7e6060c0b8d48459964df6e1eb524c03">Vec3b</a>(90, 100, 50), <a class="code" href="../../dc/d84/group__core__basic.html#ga7e6060c0b8d48459964df6e1eb524c03">Vec3b</a>(10, 20, 20));</div><div class="line">        r.<a class="code" href="../../d1/dd6/classcv_1_1RNG.html#ad26f2b09d9868cf108e84c9814aa682d">fill</a>(img(<a class="code" href="../../dc/d84/group__core__basic.html#ga11d95de507098e90bad732b9345402e8">Rect</a>(256, 256, 256, 256)), RNG::NORMAL, <a class="code" href="../../dc/d84/group__core__basic.html#ga7e6060c0b8d48459964df6e1eb524c03">Vec3b</a>(100, 10, 150), <a class="code" href="../../dc/d84/group__core__basic.html#ga7e6060c0b8d48459964df6e1eb524c03">Vec3b</a>(10, 5, 40));</div><div class="line">    }</div><div class="line">    Histo3DData h;</div><div class="line">    h.status=<span class="keyword">true</span>;</div><div class="line">    h.seuil=90;</div><div class="line">    h.threshold= h.seuil/1000000.0;</div><div class="line">    <span class="keywordtype">float</span> hRange[] = { 0, 256 };</div><div class="line">    <span class="keyword">const</span> <span class="keywordtype">float</span>* etendu[] = { hRange, hRange,hRange };</div><div class="line">    <span class="keywordtype">int</span> hBins = 32;</div><div class="line">    <span class="keywordtype">int</span> histSize[] = { hBins, hBins , hBins  };</div><div class="line">    <span class="keywordtype">int</span> channel[] = { 2, 1,0 };</div><div class="line">    <a class="code" href="../../d6/dc7/group__imgproc__hist.html#ga4b2b5fd75503ff9e6844cc4dcdaed35d">calcHist</a>(&amp;img, 1, channel, <a class="code" href="../../d3/d63/classcv_1_1Mat.html">Mat</a>(), h.histogram, 3, histSize, etendu, <span class="keyword">true</span>, <span class="keyword">false</span>);</div><div class="line">    <a class="code" href="../../dc/d84/group__core__basic.html#ga1b6a396a456c8b6c6e4afd8591560d80">normalize</a>(h.histogram, h.histogram, 100.0/(img.total()), 0, <a class="code" href="../../d2/de8/group__core__array.html#ggad12cefbcb5291cf958a85b4b67b6149fa9f0c1c342a18114d47b516a88e29822e">NORM_MINMAX</a>, -1, <a class="code" href="../../d3/d63/classcv_1_1Mat.html">Mat</a>());</div><div class="line">    <a class="code" href="../../d2/de8/group__core__array.html#ga7622c466c628a75d9ed008b42250a73f">minMaxIdx</a>(h.histogram,NULL,&amp;h.maxH,NULL,NULL);</div><div class="line">    <a class="code" href="../../d7/dfc/group__highgui.html#ga5afdf8410934fd099df85c75b2e0888b">namedWindow</a>(<span class="stringliteral">&quot;Image&quot;</span>);</div><div class="line">    <a class="code" href="../../d7/dfc/group__highgui.html#ga453d42fe4cb60e5723281a89973ee563">imshow</a>(<span class="stringliteral">&quot;Image&quot;</span>,img);</div><div class="line">    AddSlidebar(<span class="stringliteral">&quot;threshold&quot;</span>,<span class="stringliteral">&quot;Image&quot;</span>,0,100,h.seuil,&amp;h.seuil, UpdateThreshold,&amp;h);</div><div class="line">    <a class="code" href="../../d7/dfc/group__highgui.html#ga5628525ad33f52eab17feebcfba38bd7">waitKey</a>(30);</div><div class="line">    h.fen3D = makePtr&lt;viz::Viz3d&gt;(<span class="stringliteral">&quot;3D Histogram&quot;</span>);</div><div class="line">    h.nbWidget=0;</div><div class="line">    h.fen3D-&gt;registerKeyboardCallback(KeyboardViz3d,&amp;h);</div><div class="line">    DrawHistogram3D(h);</div><div class="line">    <span class="keywordflow">while</span> (h.code!=27)</div><div class="line">    {</div><div class="line">        h.fen3D-&gt;spinOnce(1);</div><div class="line">        <span class="keywordflow">if</span> (h.status)</div><div class="line">            DrawHistogram3D(h);</div><div class="line">        <span class="keywordflow">if</span> (h.code!=27)</div><div class="line">            h.code= <a class="code" href="../../d7/dfc/group__highgui.html#ga5628525ad33f52eab17feebcfba38bd7">waitKey</a>(30);</div><div class="line">    }</div><div class="line">    <span class="keywordflow">return</span> 0;</div><div class="line">}</div><div class="line"><span class="preprocessor">#else</span></div><div class="line"></div><div class="line"><span class="keywordtype">int</span> main(<span class="keywordtype">int</span> argc, <span class="keywordtype">char</span> **argv)</div><div class="line">{</div><div class="line">cout &lt;&lt; <span class="stringliteral">&quot; you need VIZ module\n&quot;</span>;</div><div class="line"><span class="keywordflow">return</span> 0;</div><div class="line">}</div><div class="line"><span class="preprocessor">#endif</span></div></div><!-- fragment --><h2>Explanation </h2>
<p>Here is the general structure of the program:</p>
<ul>
<li><p class="startli">You can give full path to an image in command line </p><div class="fragment"><div class="line">    CommandLineParser parser(argc, argv, keys);</div><div class="line"></div><div class="line">    <span class="keywordflow">if</span> (parser.has(<span class="stringliteral">&quot;help&quot;</span>))</div><div class="line">    {</div><div class="line">        parser.printMessage();</div><div class="line">        <span class="keywordflow">return</span> 0;</div><div class="line">    }</div><div class="line">    <a class="code" href="../../dc/d84/group__core__basic.html#ga1f6634802eeadfd7245bc75cf3e216c2">String</a> nomFic = parser.get&lt;<a class="code" href="../../dc/d84/group__core__basic.html#ga1f6634802eeadfd7245bc75cf3e216c2">String</a>&gt;(0);</div><div class="line">    Mat img;</div><div class="line">    <span class="keywordflow">if</span> (nomFic.length() != 0)</div><div class="line">    {</div><div class="line">        img = <a class="code" href="../../d4/da8/group__imgcodecs.html#ga288b8b3da0892bd651fce07b3bbd3a56">imread</a>(nomFic, <a class="code" href="../../d8/d6a/group__imgcodecs__flags.html#gga61d9b0126a3e57d9277ac48327799c80af660544735200cbe942eea09232eb822">IMREAD_COLOR</a>);</div><div class="line">        <span class="keywordflow">if</span> (img.empty())</div><div class="line">        {</div><div class="line">            cout &lt;&lt; <span class="stringliteral">&quot;Image does not exist!&quot;</span>;</div><div class="line">            <span class="keywordflow">return</span> 0;</div><div class="line">        }</div><div class="line">    }</div></div><!-- fragment --><p> or without path, a synthetic image is generated with pixel values are a gaussian distribution <a class="el" href="../../d1/dd6/classcv_1_1RNG.html#ad26f2b09d9868cf108e84c9814aa682d">cv::RNG::fill</a> center(60+/-10,40+/-5,50+/-20) in first quadrant, (160+/-20,10+/-5,50+/-10) in second quadrant, (90+/-10,100+/-20,50+/-20) in third quadrant, (100+/-10,10+/-5,150+/-40) in last quadrant. </p><div class="fragment"><div class="line">    <span class="keywordflow">else</span></div><div class="line">    {</div><div class="line">        img = Mat(512,512,<a class="code" href="../../d1/d1b/group__core__hal__interface.html#ga88c4cd9de76f678f33928ef1e3f96047">CV_8UC3</a>);</div><div class="line">        parser.printMessage();</div><div class="line">        RNG r;</div><div class="line">        r.<a class="code" href="../../d1/dd6/classcv_1_1RNG.html#ad26f2b09d9868cf108e84c9814aa682d">fill</a>(img(<a class="code" href="../../dc/d84/group__core__basic.html#ga11d95de507098e90bad732b9345402e8">Rect</a>(0, 0, 256, 256)), RNG::NORMAL, <a class="code" href="../../dc/d84/group__core__basic.html#ga7e6060c0b8d48459964df6e1eb524c03">Vec3b</a>(60, 40, 50), <a class="code" href="../../dc/d84/group__core__basic.html#ga7e6060c0b8d48459964df6e1eb524c03">Vec3b</a>(10, 5, 20));</div><div class="line">        r.fill(img(<a class="code" href="../../dc/d84/group__core__basic.html#ga11d95de507098e90bad732b9345402e8">Rect</a>(256, 0, 256, 256)), RNG::NORMAL, <a class="code" href="../../dc/d84/group__core__basic.html#ga7e6060c0b8d48459964df6e1eb524c03">Vec3b</a>(160, 10, 50), <a class="code" href="../../dc/d84/group__core__basic.html#ga7e6060c0b8d48459964df6e1eb524c03">Vec3b</a>(20, 5, 10));</div><div class="line">        r.fill(img(<a class="code" href="../../dc/d84/group__core__basic.html#ga11d95de507098e90bad732b9345402e8">Rect</a>(0, 256, 256, 256)), RNG::NORMAL, <a class="code" href="../../dc/d84/group__core__basic.html#ga7e6060c0b8d48459964df6e1eb524c03">Vec3b</a>(90, 100, 50), <a class="code" href="../../dc/d84/group__core__basic.html#ga7e6060c0b8d48459964df6e1eb524c03">Vec3b</a>(10, 20, 20));</div><div class="line">        r.fill(img(<a class="code" href="../../dc/d84/group__core__basic.html#ga11d95de507098e90bad732b9345402e8">Rect</a>(256, 256, 256, 256)), RNG::NORMAL, <a class="code" href="../../dc/d84/group__core__basic.html#ga7e6060c0b8d48459964df6e1eb524c03">Vec3b</a>(100, 10, 150), <a class="code" href="../../dc/d84/group__core__basic.html#ga7e6060c0b8d48459964df6e1eb524c03">Vec3b</a>(10, 5, 40));</div><div class="line">    }</div></div><!-- fragment --><p> Image tridimensional histogram is calculated using opencv <a class="el" href="../../d6/dc7/group__imgproc__hist.html#ga4b2b5fd75503ff9e6844cc4dcdaed35d">cv::calcHist</a> and <a class="el" href="../../dc/d84/group__core__basic.html#ga1b6a396a456c8b6c6e4afd8591560d80">cv::normalize</a> between 0 and 100. </p><div class="fragment"><div class="line">    Histo3DData h;</div><div class="line">    h.status=<span class="keyword">true</span>;</div><div class="line">    h.seuil=90;</div><div class="line">    h.threshold= h.seuil/1000000.0;</div><div class="line">    <span class="keywordtype">float</span> hRange[] = { 0, 256 };</div><div class="line">    <span class="keyword">const</span> <span class="keywordtype">float</span>* etendu[] = { hRange, hRange,hRange };</div><div class="line">    <span class="keywordtype">int</span> hBins = 32;</div><div class="line">    <span class="keywordtype">int</span> histSize[] = { hBins, hBins , hBins  };</div><div class="line">    <span class="keywordtype">int</span> channel[] = { 2, 1,0 };</div><div class="line">    <a class="code" href="../../d6/dc7/group__imgproc__hist.html#ga4b2b5fd75503ff9e6844cc4dcdaed35d">calcHist</a>(&amp;img, 1, channel, Mat(), h.histogram, 3, histSize, etendu, <span class="keyword">true</span>, <span class="keyword">false</span>);</div><div class="line">    <a class="code" href="../../dc/d84/group__core__basic.html#ga1b6a396a456c8b6c6e4afd8591560d80">normalize</a>(h.histogram, h.histogram, 100.0/(img.total()), 0, <a class="code" href="../../d2/de8/group__core__array.html#ggad12cefbcb5291cf958a85b4b67b6149fa9f0c1c342a18114d47b516a88e29822e">NORM_MINMAX</a>, -1, Mat());</div><div class="line">    <a class="code" href="../../d2/de8/group__core__array.html#ga7622c466c628a75d9ed008b42250a73f">minMaxIdx</a>(h.histogram,NULL,&amp;h.maxH,NULL,NULL);</div></div><!-- fragment --><p> channel are 2, 1 and 0 to synchronise color with Viz axis color in objetc <a class="el" href="../../df/d63/classcv_1_1viz_1_1WCoordinateSystem.html" title="Compound widgets. ">cv::viz::WCoordinateSystem</a>.</p>
<p class="startli">A slidebar is inserted in image window. Init slidebar value is 90, it means that only histogram cell greater than 9/100000.0 (23 pixels for an 512X512 pixels) will be display. </p><div class="fragment"><div class="line">    <a class="code" href="../../d7/dfc/group__highgui.html#ga5afdf8410934fd099df85c75b2e0888b">namedWindow</a>(<span class="stringliteral">&quot;Image&quot;</span>);</div><div class="line">    <a class="code" href="../../d7/dfc/group__highgui.html#ga453d42fe4cb60e5723281a89973ee563">imshow</a>(<span class="stringliteral">&quot;Image&quot;</span>,img);</div><div class="line">    AddSlidebar(<span class="stringliteral">&quot;threshold&quot;</span>,<span class="stringliteral">&quot;Image&quot;</span>,0,100,h.seuil,&amp;h.seuil, UpdateThreshold,&amp;h);</div><div class="line">    <a class="code" href="../../d7/dfc/group__highgui.html#ga5628525ad33f52eab17feebcfba38bd7">waitKey</a>(30);</div></div><!-- fragment --><p> We are ready to open a viz window with a callback function to capture keyboard event in viz window. Using <a class="el" href="../../d6/d32/classcv_1_1viz_1_1Viz3d.html#a481d977ad11a63da44ece08e7f76fdef">cv::viz::Viz3d::spinOnce</a> enable keyboard event to be capture in <a class="el" href="../../d7/dfc/group__highgui.html#ga453d42fe4cb60e5723281a89973ee563">cv::imshow</a> window too. </p><div class="fragment"><div class="line">    h.fen3D = makePtr&lt;viz::Viz3d&gt;(<span class="stringliteral">&quot;3D Histogram&quot;</span>);</div><div class="line">    h.nbWidget=0;</div><div class="line">    h.fen3D-&gt;registerKeyboardCallback(KeyboardViz3d,&amp;h);</div><div class="line">    DrawHistogram3D(h);</div><div class="line">    <span class="keywordflow">while</span> (h.code!=27)</div><div class="line">    {</div><div class="line">        h.fen3D-&gt;spinOnce(1);</div><div class="line">        <span class="keywordflow">if</span> (h.status)</div><div class="line">            DrawHistogram3D(h);</div><div class="line">        <span class="keywordflow">if</span> (h.code!=27)</div><div class="line">            h.code= <a class="code" href="../../d7/dfc/group__highgui.html#ga5628525ad33f52eab17feebcfba38bd7">waitKey</a>(30);</div><div class="line">    }</div></div><!-- fragment --><p> The function DrawHistogram3D processes histogram Mat to display it in a Viz window. Number of plan, row and column in <a class="el" href="../../d3/d63/classcv_1_1Mat.html#CVMat_Details">three dimensional Mat</a> can be found using this code : </p><div class="fragment"><div class="line">    <span class="keywordtype">int</span> planSize = (int)h.histogram.step1(0);</div><div class="line">    <span class="keywordtype">int</span> cols = (int)h.histogram.step1(1);</div><div class="line">    <span class="keywordtype">int</span> rows = (int)planSize / cols;</div><div class="line">    <span class="keywordtype">int</span> plans = (int)h.histogram.total() / planSize;</div><div class="line">    h.fen3D-&gt;removeAllWidgets();</div><div class="line">    h.nbWidget=0;</div><div class="line">    <span class="keywordflow">if</span> (h.nbWidget==0)</div><div class="line">        h.fen3D-&gt;showWidget(<span class="stringliteral">&quot;Axis&quot;</span>, viz::WCoordinateSystem(10));</div></div><!-- fragment --><p> To get histogram value at a specific location we use <a class="el" href="../../d3/d63/classcv_1_1Mat.html#a305829ed5c0ecfef7b44db18953048e8">cv::Mat::at(int i0,int i1, int i2)</a> method with three arguments k, i and j where k is plane number, i row number and j column number. </p><div class="fragment"><div class="line">    <span class="keywordflow">for</span> (<span class="keywordtype">int</span> k = 0; k &lt; plans; k++)</div><div class="line">    {</div><div class="line">        <span class="keywordflow">for</span> (<span class="keywordtype">int</span> i = 0; i &lt; rows; i++)</div><div class="line">        {</div><div class="line">            <span class="keywordflow">for</span> (<span class="keywordtype">int</span> j = 0; j &lt; cols; j++)</div><div class="line">            {</div><div class="line">                <span class="keywordtype">double</span> x = h.histogram.at&lt;<span class="keywordtype">float</span>&gt;(k, i, j);</div><div class="line">                <span class="keywordflow">if</span> (x &gt;= h.threshold)</div><div class="line">                {</div><div class="line">                    <span class="keywordtype">double</span> r=<a class="code" href="../../d7/dcc/group__core__utils__softfloat.html#ga78f988f6cfa6223610298cbd4f86ec66">std::max</a>(x/h.maxH,0.1);</div><div class="line">                    viz::WCube s(<a class="code" href="../../dc/d84/group__core__basic.html#ga17f8af95b76535bd387f4909850c8810">Point3d</a>(k - r / 2, i - r / 2, j - r / 2), <a class="code" href="../../dc/d84/group__core__basic.html#ga17f8af95b76535bd387f4909850c8810">Point3d</a>(k + r / 2, i + r / 2, j + r / 2), <span class="keyword">false</span>, viz::Color(j / <span class="keywordtype">double</span>(plans) * 255, i / <span class="keywordtype">double</span>(rows) * 255, k / <span class="keywordtype">double</span>(cols) * 255));</div><div class="line">                    h.fen3D-&gt;showWidget(format(<span class="stringliteral">&quot;I3d%d&quot;</span>, h.nbWidget++), s);</div><div class="line">                }</div><div class="line">            }</div><div class="line">        }</div><div class="line">    }</div></div><!-- fragment --></li>
<li>Callback function Principle are as mouse callback function. Key code pressed is in field code of class <a class="el" href="../../d3/d06/classcv_1_1viz_1_1KeyboardEvent.html">cv::viz::KeyboardEvent</a>. <div class="fragment"><div class="line"><span class="keywordtype">void</span>  KeyboardViz3d(<span class="keyword">const</span> viz::KeyboardEvent &amp;w, <span class="keywordtype">void</span> *t)</div><div class="line">{</div><div class="line">   Histo3DData *x=(Histo3DData *)t;</div><div class="line">   <span class="keywordflow">if</span> (w.action)</div><div class="line">       cout &lt;&lt; <span class="stringliteral">&quot;you pressed &quot;</span>&lt;&lt; w.symbol&lt;&lt; <span class="stringliteral">&quot; in viz window &quot;</span>&lt;&lt;x-&gt;fen3D-&gt;getWindowName()&lt;&lt;<span class="stringliteral">&quot;\n&quot;</span>;</div><div class="line">   x-&gt;code= w.code;</div><div class="line">   <span class="keywordflow">switch</span> (w.code) {</div><div class="line">   <span class="keywordflow">case</span> <span class="charliteral">&#39;/&#39;</span>:</div><div class="line">           x-&gt;status=<span class="keyword">true</span>;</div><div class="line">           x-&gt;threshold *= 0.9;</div><div class="line">       <span class="keywordflow">break</span>;</div><div class="line">   <span class="keywordflow">case</span> <span class="charliteral">&#39;*&#39;</span>:</div><div class="line">       x-&gt;status = <span class="keyword">true</span>;</div><div class="line">           x-&gt;threshold *= 1.1;</div><div class="line">       <span class="keywordflow">break</span>;</div><div class="line">    }</div><div class="line">   <span class="keywordflow">if</span> (x-&gt;status)</div><div class="line">   {</div><div class="line">       cout &lt;&lt;  x-&gt;threshold &lt;&lt; <span class="stringliteral">&quot;\n&quot;</span>;</div><div class="line">       DrawHistogram3D(*x);</div><div class="line">   }</div><div class="line">}</div></div><!-- fragment --> <h2>Results </h2>
</li>
</ul>
<p>Here is the result of the program with no argument and threshold equal to 50.</p>
<div class="image">
<img src="../../histo50.png" alt="histo50.png"/>
</div>
 </div></div><!-- contents -->
<!-- HTML footer for doxygen 1.8.6-->
<!-- start footer part -->
<hr class="footer"/><address class="footer"><small>
Generated on Fri Apr 2 2021 11:36:39 for OpenCV by &#160;<a href="http://www.doxygen.org/index.html">
<img class="footer" src="../../doxygen.png" alt="doxygen"/>
</a> 1.8.13
</small></address>
<script type="text/javascript">
//<![CDATA[
addTutorialsButtons();
//]]>
</script>
</body>
</html>
